<?php

namespace App\Http\Controllers\Home;

use App\Http\Model\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;

require_once 'public/org/Code/Code.php';
class LoginController extends Controller
{
    public function index(){
        return view('home.login');
    }

    public function register(){
        if($input = Input::all()){
//            $input['formData']= unserialize($input['formData']);
//            dd($input->except('_token'));
            parse_str($input['formData'], $output);//将字符串转换为数组
//            dd($output);
//            dd($input['username']);
            //验证规则
            $rules = [
                'username'=>'required',
                'userphone'=>'required',
                'userphone'=>'regex:/^1[34578][0-9]{9}$/',
                'yanzhengma'=>'required',
                'password'=>'required',
                'agree'=>'required',
            ];
            //错误信息
            $message = [
                'username.required'=>'真实姓名不能为空',
                'userphone.required'=>'手机号不能为空',
                'yanzhengma.required'=>'验证码不能为空',
                'password.required'=>'密码不能为空',
                'agree.required'=>'必须同意协议',
            ];
            $validator = Validator::make($output,$rules,$message);

            if($validator->passes()){
                $tel = DB::table('user')->value('accounts');//获得user表account字段
                if($tel == $output['userphone']){
                    $data = [
                        'status'=>1,
                        'messgae'=>'用户已存在',
                    ];
                    return $data;
                }

                $userData = [
                    'accounts'=>$output['userphone'],
                    'password'=>Crypt::encrypt($output['password']),
                    'username'=>$output['username'],
                    'sendtime'=>time(),
                ];

               $re =  User::create($userData);

                if($re){
                    $data = [
                        'status'=>0,
                        'messgae'=>'添加成功',
                    ];
                    return $data;
                }else{
                    $data = [
                        'status'=>1,
                        'messgae'=>'添加失败',
                    ];
                    return $data;
                }



            }else{
                return back()->withErrors($validator);
            }
        }
        return view('home.register');
    }

    //验证码
    public function code(){
        $code = new \App\Resources\Org\Code();
        $code->make();
    }

    //登录
    public function login(){
        if($input = Input::all()){
//            dd($input);
            if(!$input['accounts']){
                $data = [
                    'status'=>0,
                    'message'=>'账户不能为空',
                ];

                return $data;
            }

            if(!$input['password']){
                $data = [
                    'status'=>0,
                    'message'=>'密码不能为空',
                ];

                return $data;
            }

            //查找该用户
            $account = DB::table('user')->where('accounts',$input['accounts'])->get();
            if(!$account){
                $data = [
                    'status'=>0,
                    'message'=>'该用户不存在',
                ];

                return $data;
            }

            ;
            //解密
            $newpass = Crypt::decrypt($account[0]->password);
            if($newpass == $input['password']){
                $data = [
                    'status'=>1,
                    'message'=>'登录成功',
                ];
               $uid  =  DB::table('user')->where('accounts',$input['accounts'])->value('uid');
                $uname  =  DB::table('user')->where('accounts',$input['accounts'])->value('username');
                session(['uid'=>$uid]);
                session(['uname'=>$uname]);
//                dd($data);
                return $data;
            }else{
                $data = [
                    'status'=>0,
                    'message'=>'密码错误',
                ];
                return $data;
            }



        }
    }
}
